JLCA Companion Reference

ZipUtil.java

 
/**
 * ZipUtil.java
 *
 * @author  Artinsoft
 */
import java.util.zip.*;
import java.util.*;
import java.io.*;

public class ZipUtil {

    /** Creates a new instance of ZipUtil */
    public ZipUtil() {
    }

  public static final void main(String[] args) {

    if(args.length != 2) {
      System.out.println("Usage: ZipUtil [/options] file|directory");
      System.out.println("");
      System.out.println("where options include:");
      System.out.println("/list         to list the contents of the specified zip file");
      System.out.println("/unzip        retrieves  the compressed files from the specified zip file");
      System.out.println("/zip          compresses the specified file or directory into a zip file");
      System.out.println("/checksum     calculates the checksum of the specified file");
      return;
    }

    String action =  args[0];

    if (action.equals("/list") || action.equals("/unzip")){
        ZipFileExtended zf = null;
        try{
            zf = new ZipFileExtended(args[1]);
            if (action.equals("/list"))
                System.out.println(zf.toString());
            else if (action.equals("/unzip"))
                zf.unZip();
        }
        catch(Exception e){
            System.out.println("Error in file "+ args[1] +". Exception: " + e.getMessage());
        }

    } else if (action.equals("/zip")){
        try{
            ZipOutExtended zip = new ZipOutExtended(args[1],args[1]);
            zip.flush();
            zip.close();
        }catch(Exception e){
            System.out.println("Error zipping directory. Exception: " + e.getMessage());
        }

    }

  }
}

©2004 ArtinSoft. All rights reserved